//
//  EFMyCollectionViewController.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/8.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFMyCollectionViewController.h"
#import "EFCouponSegmentView.h"
#import "EFMyCollectionGoodsViewItem.h"
#import "EFMyCollectionNoteViewItem.h"
#import "iToast.h"

@interface EFMyCollectionViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(strong,nonatomic)EFCouponSegmentView* segmentView;
@property(nonatomic, strong)UIScrollView *scrollView;

@property(strong,nonatomic)NSMutableArray* dataArray1;
@property(strong,nonatomic)UITableView* tableView1;

//@property(strong,nonatomic)NSMutableArray* dataArray2;
//@property(strong,nonatomic)UITableView* tableView2;

@end

@implementation EFMyCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"我的收藏";
        self.dataArray1 = [NSMutableArray array];
//        self.dataArray2 = [NSMutableArray array];
    }
    return self;
}


-(void)initView{
    __weak typeof(self) weakSelf = self;
    [self.segmentView setSegmentArray:@[@"商品"] SelectIndex:^(NSInteger index) {
        [weakSelf clickIndex:index];
    }];
//    [self.segmentView setSegmentArray:@[@"商品",@"帖子"] SelectIndex:^(NSInteger index) {
//        [weakSelf clickIndex:index];
//    }];
    [self.view addSubview:self.segmentView];
    [self.scrollView addSubview:self.tableView1];
//    [self.scrollView addSubview:self.tableView2];
    
    [self.view addSubview:self.scrollView];
    
    
    [self clickIndex:0];
//    [self requestCollectNotes];
    
}

- (void)clickIndex:(NSInteger)index{
    [UIView animateWithDuration:0.3f animations:^{
        self.scrollView.contentOffset = CGPointMake(index *kScreenWidth, 0);
    } completion:nil];
    switch (index) {
        case 0:{
            if (self.dataArray1.count == 0) {
                [self requestCollectGoods];
            }
            break;
        }
//        case 1:{
//            if (self.dataArray2.count == 0) {
//                [self requestCollectNotes];
//            }
//            break;
//        }
        default:
            break;
    }
    
}

- (void)requestCollectGoods{
    
    [self showHUD];
    __weak typeof(self) weakSelf = self;
    [APP_CONTEXT.net getCollectionGoods:^(NSInteger code, NSDictionary *info, NSString *message) {
        [self.HUD hide:YES];
        if(code == 0){
            if ([info objectForKey:@"list"]) {
                NSArray *array = [info objectForKey:@"list"];
                if (array.count == 0) {
                    [weakSelf showEmptyImageView:@"bg_icon_collect" message:@"您还未收藏任何内容"];
                }else{
                    for (NSDictionary *dic in array) {
                        EFMyCollectionGoodsViewItem *item = [EFMyCollectionGoodsViewItem yy_modelWithDictionary:dic];
                        [weakSelf.dataArray1 addObject:item];
                    }
                    [weakSelf.tableView1 reloadData];
                }
               
            }
        }else{
            [[iToast makeText:message]show];
        }
    }];
    
}

//- (void)requestCollectNotes{
//    EFMyCollectionNoteViewItem* item = [[EFMyCollectionNoteViewItem alloc] init];
//    item.title = @"旺旺 小小酥 200g 原味 25g*8小包 薯片零食礼物";
//    item.date = @"2017-01-20";
//    item.userName = @"yuan li";
//    
//    [self.dataArray2 addObject:item];
//    
//    EFMyCollectionNoteViewItem* item2 = [[EFMyCollectionNoteViewItem alloc] init];
//    item2.title = @"旺旺 小小酥 200g 原味 25g*8小包 薯片零食礼物";
//    item2.date = @"2017-01-20";
//    item2.userName = @"yuan li";
//    
//    [self.dataArray2 addObject:item2];
//    [self.tableView2 reloadData];
//}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.tableView1) {
        return self.dataArray1.count;
    }
//    else if (tableView == self.tableView2) {
//        return self.dataArray2.count;
//    }
    
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item;
    if (tableView == self.tableView1) {
        item =  self.dataArray1[indexPath.row];
    }
//    else if (tableView == self.tableView2) {
//        item =  self.dataArray2[indexPath.row];
//    }
    
    return item.viewHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item;
    if (tableView == self.tableView1) {
        item =  self.dataArray1[indexPath.row];
    }
//    else if (tableView == self.tableView2) {
//        item =  self.dataArray2[indexPath.row];
//    }
    
    NSString *identifier = NSStringFromClass(item.viewClass);
    ICTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        cell = [[item.viewClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if ([item isKindOfClass:[ICTableViewItem class]]) {
        [cell setObject:item];
    }
    return cell;
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView == self.tableView1){
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            ICTableViewItem* item = self.dataArray1[indexPath.row];
            EFMyCollectionGoodsViewItem* goodItem = (EFMyCollectionGoodsViewItem*)item;
            [APP_CONTEXT.net removeCollect:goodItem.id OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
                if(code == 0){
                    [self.dataArray1 removeObjectAtIndex:indexPath.row];
                    // Delete the row from the data source.
                    [self.tableView1 deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                    if (self.dataArray1.count == 0) {
                        [self showEmptyImageView:@"bg_icon_collect" message:@"您还未收藏任何内容"];
                    }
                }else{
                    [[iToast makeText:message] show];
                }
            }];

        }
    }
//    else if (tableView == self.tableView2){
//        if (editingStyle == UITableViewCellEditingStyleDelete) {
//            [self.dataArray2 removeObjectAtIndex:indexPath.row];
//            // Delete the row from the data source.
//            [self.tableView2 deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//        }
//    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger index = self.scrollView.contentOffset.x/kScreenWidth;
    [self.segmentView setSeletIndex:index];
}


- (UITableView *)tableView1{
    if (!_tableView1) {
        _tableView1 = [self getATableView];
    }
    return _tableView1;
}

//- (UITableView *)tableView2{
//    if (!_tableView2) {
//        _tableView2 = [self getATableView];
//        _tableView2.left = kScreenWidth;
//    }
//    return _tableView2;
//}


- (UITableView *)getATableView{
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kStatusBarHeight -32.f)];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.contentInset = UIEdgeInsetsMake(15.f, 0, 0, 0);
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor clearColor];
    return tableView;
}

-(EFCouponSegmentView*)segmentView{
    if(_segmentView == NULL){
        _segmentView = [[EFCouponSegmentView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 32)];
    }
    return _segmentView;
}


- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 32, kScreenWidth, kScreenHeight - kStatusBarHeight - 32)];
        _scrollView.contentSize = CGSizeMake(kScreenWidth , _scrollView.height);
//        _scrollView.contentSize = CGSizeMake(kScreenWidth * 2, _scrollView.height);
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.scrollEnabled = NO;
    }
    return _scrollView;
}

@end
